#include <stdio.h>
#include <time.h>
#include "Customer.h"
#include "customer/CustomerService.h"
#include "address/AddressService.h"

char* printDate(time_t t)
{
    struct tm* local_time = localtime(&t);
    char* buf;
    sprintf(buf, "%04d-%02d-%02d", local_time->tm_year+1900,local_time->tm_mon+1,local_time->tm_mday);
    return buf;
}

void printCustomer(struct Customer* customer)
{
    if (customer==NULL)
    {
        printf("Customer: {}\n");
    } else
    {
        char* gender = (customer->gender==MALE) ? (char*)"male" : (char*)"famale";
        printf("Customer: {id:%ld,name:%s,gender:%s,birthdate:%s,identification:%s,phoneNumber:%s}\n", 
            customer->id, customer->name, gender, printDate(customer->birthdate), 
            customer->identification, customer->phoneNumber);
    }
}

int testCustomer()
{
    printf(">>> begin test customer:");
    long customerId = 1L;
    struct Customer customer = {customerId,(char*)"John",MALE,time(0),(char*)"510212200012137812",(char*)"13456567878"};
    
    int rtn = CustomerService_register(&customer);
    if(rtn!=0) return rtn;
    struct Customer* result = CustomerService_load(customerId);
    printCustomer(result);
    
    result->name = (char*)"Mary";
    result->gender = FAMALE;
    rtn = CustomerService_modify(result);
    if(rtn!=0) return rtn;
    result = CustomerService_load(customerId);
    printCustomer(result);

    rtn = CustomerService_remove(customerId);
    if(rtn!=0) return rtn;
    result = CustomerService_load(customerId);
    printCustomer(result);
    return rtn;
}

void printAddress(struct Address* address)
{
    if (address==NULL)
    {
        printf("Address: {}\n");
    } else
    {
        printf("Address: {id:%ld,customerId:%ld,country:%s,province:%s,city:%s,zone:%s,address:%s,phoneNumber:%s}\n", 
            address->id, address->customerId, address->country, address->province, address->city, address->zone, 
            address->address, address->phoneNumber);
    }
}

int testAddress()
{
    printf(">>> begin test address:");
    long addressId = 1L;
    long customerId = 1L;
    struct Address address = {id:addressId,customerId:customerId,country:(char*)"China",province:(char*)"Shandong",
        city:(char*)"Jinan",zone:(char*)"Lixiaqu",address:(char*)"Happy street No.12",phoneNumber:(char*)"0531-88896666"};

    struct Customer customer = {customerId,(char*)"John",MALE,time(0),(char*)"510212200012137812",(char*)"13456567878"};
    CustomerService_register(&customer);

    int rtn = AddressService_create(&address);
    if(rtn!=0) return rtn;
    struct Address* result = AddressService_load(addressId);
    printAddress(result);
    
    rtn = AddressService_modify(result);
    if(rtn!=0) return rtn;
    result = AddressService_load(addressId);
    printAddress(result);

    rtn = AddressService_remove(addressId);
    if(rtn!=0) return rtn;
    result = AddressService_load(addressId);
    printAddress(result);
    return rtn;
}

int main()
{
    int rtn = testCustomer();
    if(rtn!=0) return rtn;
    rtn = testAddress();
    if(rtn!=0) return rtn;
    return 0;
}
